05. Variable or Constant?

Variable or Constant

Prepare

For this exercise, we are going to look at descriptions of values that you might use in an app. It will be your job to determine whether a variable or constant should be used. When choosing between the two, Apple says: if a stored value is not going to change, always declare it as a constant with the let keyword. Use variables only for storing values that need to be able to change.

Learn

Variable or Constant Quiz

QUESTION:

For the following descriptions, decide whether they should be represented as a variable or a constant.

  • I am an Int value representing the hunger level of an animal over time.
  • I am an Int value representing the score always required to win the game.
  • I am a String value representing a student’s birthday.
  • I am a Int value representing the largest prime number less than 10.
  • I am a String value representing what a user types into the status update area.
ANSWER:

Here are our answers:

  • I am an Int value representing the hunger level of an animal over time.
    • We know the hunger of an animal can change over time. For example, if an animal is eating a meal, then its hunger can go from starving to content very quickly. So this should be a variable.
  • I am an Int value representing the score always required to win the game.
    • The keyword is “always”. This value never changes, it is always the same. Therefore, it should be a constant.
  • I am a String value representing a student’s birthday.
    • We know that birthdays do not change, so this should be a constant.
  • I am a Int value representing the largest prime number less than 10.
    • For you math folks out there, the largest prime number less than 10 is 7. And, unless the definition for prime numbers changes anytime soon, then this value is constant.
  • I am a String value representing what a user types into the status update area.
    • As the user types, this value will change. Therefore, it should be a variable.

Guess that Type!

Prepare

Now we are going to play a little game of “Guess That Type”. Here are a few declarations in Swift.

Types in Playground

These declarations do not specify their types, so the Swift compiler infers them!

These declarations do not specify their types, so the Swift compiler infers them!

Guess that Type!

Notice that we have left out the explicit type. Hence, the Swift compiler will try to infer the type based on the value that is used.

Learn

Guess the Types Quiz

QUESTION:

Instead of the Swift compiler inferring the type, we want you to guess the data types. So look at the name and value of each, and guess the data type. Also, if there are values you’ve never seen before, then choose any word that you think best describes the value.

  • firstName
  • alarmSet
  • attackBonus1
  • attackBonus2
  • hairColor
ANSWER:

Here are the data types that the Swift compiler used:

  • firstName is a String
    • Since Jarrod is set off inside of quotes, the compiler knows this is a String.
  • alarmSet is a Bool
    • The compiler knows this because a Bool value can be either true or false.
  • attackBonus1 is an Integer
    • The value 20 is a whole number. Therefore, the compiler uses Integer.
  • attackBonus2 is a Double
    • You may have thought this value is a Float, but Double is the correct answer. Whenever the Swift compiler sees a floating-point number without an explicit type it uses Double because it has higher precision.
  • hairColor is a UIColor
    • Anything related to Color or Colour (for our friends in the UK & Canada) would have been a great guess! Technically, hairColor is a UIColor which is a special data type used to define colors in UIKit. You'll see plenty more of UIKit later when you start building app interfaces.

Valid Names

Prepare

Now we are going to look at a list of variable and constant names. It will be your job to determine which are valid and which are invalid.

Valid Names Quiz

QUESTION:

To complete the exercise, correctly mark which names are valid and which are invalid. You can use an Xcode Playground to check your answers.

  • var
  • import
  • _temperature
  • $valueOfDollar
  • 1stplace
  • valueOfDollar$
  • _name_of_file_
ANSWER:

I hope you took a moment to check these names in a Playground. Here are the correct answers:

  • var is invalid
    • var is a keyword in Swift for declaring variables
  • import is invalid
    • import is a keyword in Swift for reusing existing code
  • _temperature is valid
    • names can start with underscores
  • $valueOfDollar is invalid
    • names cannot begin with dollar signs
  • 1stplace is invalid
    • names cannot begin with numbers
  • valueOfDollar$ is valid
    • names can include most symbols, as long as they are not the first character
  • _name_of_file_ is valid
    • names can include (and begin) with underscore

Playing with Variables

For the final exercise, try out the Variables Exercises playground from the Beginning iOS Playground Collection and complete the exercises contained within it. If you're stuck at any point, head to forums to get help from iOS coaches!

Variables Outro